<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>渐变画图</title>
    <style>
        /*不能在里面设置宽高*/
        canvas {
            border: 1px solid black;
        }
    </style>
</head>
<body>
<button id="bt">点击绘画</button>
<hr>

</body>


<script>
    const canvas = document.createElement('canvas');
    document.body.append(canvas)
    canvas.width = 600
    canvas.height = 400
    const ctx = canvas.getContext('2d')

    const geneateCanvas = () => {
        //暂停2s后重新绘制
        setTimeout(() => {

            const linearGradient = ctx.createLinearGradient(0, 0, 600, 400);
            linearGradient.addColorStop(0, 'red');
            linearGradient.addColorStop(1, 'blue');
            ctx.lineWidth = 15;

            //修改一个直线开头样式,具体看mdn文档
            ctx.lineCap='round';
            //修改连接点处样式
            ctx.lineJoin = 'round';
            ctx.moveTo(100, 100);
            ctx.lineTo(200, 200);
            ctx.lineTo(300, 80);
            ctx.lineTo(350, 320);
            ctx.strokeStyle = linearGradient;
            ctx.stroke();
        }, 2000);


    }

    const button = document.getElementById('bt');
    button.addEventListener('click', geneateCanvas)


</script>
</html>